package com.upsuns.service;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import com.upsuns.util.Constants;

/**
 *@Description:<p>远程服务接口实现</p>
 *@author 王旭
 *@time 2016年3月14日 下午9:12:48
 */
public class FileServiceImp extends UnicastRemoteObject implements FileService,Serializable{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private RandomAccessFile raf;
	
	private String fileName;
	
	private long fileSize;
	
	private long perSize;
	
	private long restSize;
	
	private int times; //要传输的次数
	
	private boolean isFinished;
	
	
	public FileServiceImp(String path) throws IOException {
		File file = new File(path);
		raf = new RandomAccessFile(file, "rw");
		this.fileSize = raf.length();
		this.fileName = file.getName();
		
		//如果文件大小比一次要传输的小则一次发送完毕
		if(this.fileSize <= Constants.PER_SIZE) {
			this.perSize = this.fileSize;
			this.times = 1;
			this.restSize = 0;
		}
		else {
			this.perSize = Constants.PER_SIZE;
			if(this.fileSize % Constants.PER_SIZE == 0) {
				this.times = (int) (this.fileSize / Constants.PER_SIZE);
				this.restSize = 0;
			}
			else {
				this.times = (int) (this.fileSize / Constants.PER_SIZE) + 1;
				this.restSize = this.fileSize % Constants.PER_SIZE;
			}
		}
		
		this.isFinished = false;
	}

	@Override
	public byte[] readFile(int times) throws RemoteException {
		
		FileChannel fileChannel = this.raf.getChannel();
		
		MappedByteBuffer buffer = null;
		
		long pos = 0, size = 0;
		if(this.times == 1) { //一次读取完毕
			pos = 0;
			size = this.fileSize;
		}
		else {
			if(this.restSize == 0) { //正好读完
				pos = this.perSize * (times-1); //times从1开始
				size = this.perSize;
			}
			else {
				if(times == this.times) { //是最后一次
					pos = this.perSize * (times-1);
					size = this.restSize;
				}
			}
		}
		
		
		try {
			buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, pos, size);
			fileChannel.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//把数据写入到字节数组中并返回
		byte[] bufferArr = new byte[(int)size];

		buffer.get(bufferArr);
		
		return bufferArr;
	}

	@Override
	public void finished() throws RemoteException {
		System.out.println("文件传输完毕，请关闭窗口");
		this.isFinished = true;
	}
	
	@Override
	public boolean isFinished() throws RemoteException {
		return this.isFinished;
	}

	@Override
	public RandomAccessFile getRaf() throws RemoteException {
		return raf;
	}

	@Override
	public long getFileSize() throws RemoteException {
		return fileSize;
	}

	@Override
	public long getPerSize() throws RemoteException {
		return perSize;
	}

	@Override
	public long getRestSize() throws RemoteException {
		return restSize;
	}

	@Override
	public int getTimes() throws RemoteException {
		return times;
	}
	
	@Override
	public String getFileName() throws RemoteException {
		return this.fileName;
	}
	
}
